home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Sas-PPC / examples / runelf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  4.5 KB  |  170 lines

  1. #include <string.h>
  2. #include <exec/memory.h>
  3. #include <libraries/dosextens.h>
  4. #include <proto/dos.h>
  5. #include <proto/exec.h>
  6. #include <workbench/startup.h>
  7. #include <workbench/workbench.h>
  8. #include <workbench/icon.h>
  9. #include <proto/icon.h>
  10. #include <dos/dostags.h>
  11. #include <PowerUP/PPCLib/Interface.h>
  12. #include <PowerUP/PPCLib/tasks.h>
  13. #include <PowerUP/PPCLib/ppc.h>
  14. #include <PowerUP/PPCLib/object.h>
  15. #include <PowerUP/PPCDisslib/PPCDiss.h>
  16. #include <PowerUP/pragmas/ppc_pragmas.h>
  17. #include <PowerUP/clib/ppc_protos.h>
  18. #include <PowerUP/pragmas/ppcdiss_pragmas.h>
  19. #include <PowerUP/clib/ppcdiss_protos.h>
  20.  
  21. struct Library *PPCLibBase, *SysBase;
  22. struct DosLibrary *DOSBase;
  23.  
  24. __saveds __asm __main(register __a0 char *cmdline)
  25. {
  26.   void *MyObject;
  27.   char *p;
  28.   long stacksize;
  29.   struct Process *parent_process;
  30.   struct CommandLineInterface *parent_cli;
  31.   char *rdargs_line;
  32.   int rdargs_length;
  33.   char *cmdname;
  34.   int cmdlength;
  35.   ULONG    Result = -1;
  36.   struct FileHandle *CurrentInput;
  37.   ULONG OldBuffer;
  38.   ULONG OldPosition;
  39.   ULONG OldEnd;
  40.  
  41.   
  42.   SysBase = *(struct Library **)4;
  43.  
  44.   parent_process = (void *)FindTask(NULL);
  45.   parent_cli =  (void *) ((long)(parent_process->pr_CLI) << 2);
  46.   stacksize = 0x10000;
  47.   if (parent_cli)
  48.       stacksize = parent_cli->cli_DefaultStack << 2;
  49.   if (stacksize < 0x10000) stacksize = 0x10000;
  50.  
  51.  
  52.   if ((DOSBase = (void *)OpenLibrary("dos.library", 0)) == NULL)
  53.       return 20;
  54.  
  55.   if ((PPCLibBase = OpenLibrary("ppc.library",0)) == NULL)
  56.   {
  57.       CloseLibrary((void *)DOSBase);
  58.       return 20;      
  59.   }
  60.   
  61.   
  62.   /* strip off the /n the end */
  63.   p = cmdline + strlen(cmdline);
  64.   p--;
  65.   if (*p == '\n') *p = 0;
  66.  
  67.   for (p = cmdline; *p && *p != ' '; p++)
  68.   {
  69.       if (*p == '"')
  70.       {
  71.           /* search for closing " */
  72.           p++;
  73.           while (*p && *p != '"') p++;
  74.       }
  75.   }
  76.   
  77.   
  78.   cmdlength = (p - cmdline)+1;
  79.   cmdname = PPCAllocMem(cmdlength, MEMF_ANY);
  80.   if (cmdname == NULL)
  81.   {
  82.       CloseLibrary((void *)PPCLibBase);
  83.       CloseLibrary((void *)DOSBase);
  84.       return 20;
  85.   }
  86.   memcpy(cmdname, cmdline, cmdlength-1);
  87.   cmdname[cmdlength-1] = 0;
  88.   
  89.   if (*p == ' ') p++;  /* p now points to the args */
  90.   rdargs_length = strlen(p)+2;
  91.   rdargs_line = PPCAllocMem(rdargs_length, MEMF_ANY);
  92.   if (rdargs_line == NULL)
  93.   {
  94.       PPCFreeMem(cmdname, cmdlength);
  95.       CloseLibrary((void *)PPCLibBase);
  96.       CloseLibrary((void *)DOSBase);
  97.       return 20;
  98.   }
  99.   memcpy(rdargs_line, p, rdargs_length-2);
  100.   rdargs_line[rdargs_length-2] = '\n';
  101.   rdargs_line[rdargs_length-1] = '\0';
  102.   
  103.  
  104.   CurrentInput        =(struct FileHandle*) BADDR(Input());
  105.   OldBuffer        =    CurrentInput->fh_Buf;
  106.   OldPosition        =    CurrentInput->fh_Pos;
  107.   OldEnd        =    CurrentInput->fh_End;
  108.  
  109.   
  110.   if (MyObject=PPCLoadObject(cmdname))
  111.   {
  112.         static struct TagItem    MyTags[13];
  113.  
  114.         MyTags[0].ti_Tag    =    PPCTASKTAG_STOPTASK;
  115.         MyTags[0].ti_Data    =    FALSE;
  116.         MyTags[1].ti_Tag    =    PPCTASKTAG_WAITFINISH;
  117.         MyTags[1].ti_Data    =    TRUE;
  118.         MyTags[2].ti_Tag    =    PPCTASKTAG_INPUTHANDLE;
  119.         MyTags[2].ti_Data    =    (ULONG) Input();
  120.         MyTags[3].ti_Tag    =    PPCTASKTAG_OUTPUTHANDLE;
  121.         MyTags[3].ti_Data    =    (ULONG) Output();
  122.         MyTags[4].ti_Tag    =    PPCTASKTAG_ARG1;
  123.         MyTags[4].ti_Data    =    (ULONG) cmdline;
  124.         MyTags[5].ti_Tag    =    PPCTASKTAG_STACKSIZE;
  125.         MyTags[5].ti_Data    =    stacksize;
  126.         MyTags[6].ti_Tag    =    NP_CloseInput;
  127.         MyTags[6].ti_Data    =    FALSE;
  128.         MyTags[7].ti_Tag    =    NP_CloseOutput;
  129.         MyTags[7].ti_Data    =    FALSE;
  130.         MyTags[8].ti_Tag    =    PPCTASKTAG_BREAKSIGNAL;
  131.         MyTags[8].ti_Data    =    TRUE;
  132.         MyTags[9].ti_Tag    =    NP_Arguments;
  133.         MyTags[9].ti_Data    =    (ULONG)rdargs_line;
  134.         MyTags[10].ti_Tag       =       NP_Name;
  135.         MyTags[10].ti_Data      =       (ULONG)cmdname;
  136.         MyTags[11].ti_Tag       =       NP_CommandName;
  137.         MyTags[11].ti_Data      =       (ULONG)cmdname;
  138.         MyTags[12].ti_Tag    =    TAG_END;
  139.  
  140.         Result=(ULONG) PPCCreateTask(MyObject,
  141.                                      &MyTags[0]);
  142.       
  143.         UnGetC(Input(), -1);
  144.         CurrentInput->fh_Buf    =    OldBuffer;
  145.         CurrentInput->fh_Pos    =    OldPosition;
  146.         if (CurrentInput->fh_End)
  147.         {
  148.             CurrentInput->fh_End    =    OldEnd;
  149.         }
  150.  
  151.                                      
  152.         PPCUnLoadObject(MyObject);
  153.   }
  154.   else
  155.   {
  156.     if (Output())
  157.     {
  158.         Write(Output(), "Can't load '", 12);
  159.         Write(Output(), cmdline, strlen(cmdline));
  160.         Write(Output(), "'\n", 2);
  161.     }
  162.   }
  163.   
  164.   PPCFreeMem(rdargs_line, rdargs_length);
  165.   PPCFreeMem(cmdname, cmdlength);
  166.   CloseLibrary(PPCLibBase);
  167.   CloseLibrary((void *)DOSBase);
  168.   return (int)Result;
  169. }
  170.